import list from '@/assets/题库.json';
import cloneDeep from 'lodash/cloneDeep';
import random from 'lodash/random';

export default {
    namespaced: true,
    state: {
        singleCount: 10, // 单选可答题数量
        multipleCount: 10, // 多选可答题数量
        singleList: [],
        multipleList: [],
        questionNum: 0,
        score: 0,
        questionModel: 0, // 1 多选，0 单选
        nowQuestion: {},
    },
    getters: {
        maxCount: state => {
            return state.singleCount + state.multipleCount;
        },
        isLast: (state, getters) => {
            return state.questionNum + 1 >= getters.maxCount;
        },
    },
    mutations: {
        SET_SIGNLE_LIST: (state, list) => {
            state.singleList = list;
        },
        SET_MULTIPLE_LIST: (state, list) => {
            state.multipleList = list;
        },
        RESET_LIST: (state) => {
            const listCloneDeep = cloneDeep(list);
            state.singleList = listCloneDeep.filter(val => val.category == 0);
            state.multipleList = listCloneDeep.filter(val => val.category == 1);
            state.questionNum = 0;
            state.score = 0;
            state.questionModel = 0;
            state.nowQuestion = {};
        },
        SET_QUESTION_NUM: (state, num) => {
            state.questionNum = num;
            if (state.questionNum >= state.singleCount) {
                state.questionModel = 1;
            } else {
                state.questionModel = 0;
            }
        },
        SET_SCORE: (state, score) => {
            state.score = score;
            console.log('SET_SCORE', state.score);
        },
        SET_NOW_QUESTION: (state, question) => {
            state.nowQuestion = question;
        },
    },
    actions: {
        handleSelectQuestion({ state, commit }) {
            if (state.questionModel == 0) {
                const [listCloneDeep, item] = handle(state.singleList);
                commit('SET_SIGNLE_LIST', listCloneDeep);
                commit('SET_NOW_QUESTION', item);
                return item;
            } else {
                const [listCloneDeep, item] = handle(state.multipleList);
                commit('SET_MULTIPLE_LIST', listCloneDeep);
                commit('SET_NOW_QUESTION', item);
                return item;
            }
        },
        handleSubmitQuestion({ state, commit }, result) {
            commit('SET_QUESTION_NUM', state.questionNum + 1);
            if (result) {
                // console.log('handleSubmitQuestion', state.questionNum, state.singleCount);
                if (state.questionNum > state.singleCount) {
                    commit('SET_SCORE', state.score + 6); // 后10题多选 +6
                } else {
                    commit('SET_SCORE', state.score + 4); // 前10题单选 +4
                }
            }
        },
    },
};

function handle(array) {
    const listCloneDeep = cloneDeep(array);
    const [item] = listCloneDeep.splice(random(0, listCloneDeep.length - 1), 1);
    return [listCloneDeep, item];
}
